require('../css/about.less')
require('../fonts/iconfont.css')


document.ready(function () {
    // 底部footer生成
    utils.addFooter('aobut');

    //点击跳转个人消息页面
    let title = document.querySelector('.title')
    title.addEventListener('click', function () {
        location.href = '../page/edit.html'
    })

    //退出登录
    let logoff = document.querySelector('.logoff')
    logoff.addEventListener('click', function () {
        localStorage.removeItem('user')
        location.href = '../page/login.html';
    })

    //获取个人数据
    let user = JSON.parse(localStorage.getItem('user'));
    let userName = document.querySelector('.user-name')
    let content = document.querySelector('.content')
    let img = document.querySelector('img')


    //加载about页面后重新获取(更新)用户数据
    function change() {
        $http.get('/users/accountinfo?userId=' + user.userId, function (res) {
            //判断是否有头像
            //有：将img src 改成正确头像url
            //没有 显示默认头像
            if (res.data.imgurl) {
                img.src = res.data.imgurl;
            }
            //更改用户名（默认）
            userName.textContent = res.data.nickname
            //判断是否有个性签名
            //有：更改个性签名
            //没有 显示默认
            if (res.data.sign) {
                content.textContent = res.data.sign
            }
            
        })
    }
    change()
    //运动数据
    let time = document.querySelector('.time')
    let calorie = document.querySelector('.calorie')
    //请求个人中心 数据
    $http.get('/users/mysportsBadge?userId=' + user.userId, function (res) {
        time.textContent = res.data.sports.times
        calorie.textContent = res.data.sports.calorie
    })

    //点击头像更换头像图片
    let base_url = 'http://139.9.177.51:8099'
    let portrait = document.querySelector('.portrait')
    let fileBtn = document.querySelector('#file-btn')

    portrait.addEventListener('click', function (ev) {
        //不准冒！
        ev.stopPropagation()
        // 点击头像事件和点击上传文件按钮绑定
        fileBtn.click()
    })

    //监听上传文件改变事件
    fileBtn.addEventListener('change', function () {
        //文件上传--ajax

        $updateFile(base_url + '/users/upload', 'imgurl', this.files[0], function (res) {
            portrait.src = base_url + res.data;

            let data = {
                userId: user.userId,
                imgurl: base_url + res.data
            }
            $http.post('/users/userEdit?userId=' + user.userId, data, function () {
                
            })
        })


    })

    let sportData = document.querySelector('.sport-data')
    sportData.addEventListener('click',function(){
        location.href = '../page/sportData.html'
    })






})